package com.jml.zlhq.app.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.mobads.AdView;
import com.jml.zlhq.app.BuildConfig;
import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.adapter.HdDetailAdapter;
import com.jml.zlhq.app.adapter.IBaseAdapter;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundAngleImageView;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class FenxiangzhuanImageDetailActivity extends BaseActivity {
	private String id;
	private String pID;
	private List<JSONObject> listitems = new ArrayList<JSONObject>();
	private JSONObject data = new JSONObject();
	private ScrollView scrollView;
	private ListView listView;
	private IBaseAdapter adapter;
	private ImageView topImg, ising;
	private LinearLayout ewlay, ads;
	private TextView topName, topTitle, join, award, hdtime, sjinfo, jtsj,
			hdewjl, ewinfo, ewbtn, txtxbtn;
	private Button tixian_submit;
	private Button jiangli_submit;
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				ImageLoader
						.getInstance()
						.displayImage(
								URLs.IMAGE_HOST
										+ BaseTools.getFrom("image.image_url", data)
										+ "_scal_360x360.jpg",
								topImg,
								UIUtil.getImageLoaderDisplayOptions(R.drawable.icon_default_z));
				topName.setText("标题： " + BaseTools.getFrom("name", data));
				join.setText(Html.fromHtml("已有<font color='#E95847'>"
						+ BaseTools.getFrom("stats.vistors", data, "0")
						+ "</font>人参加"));
				topTitle.setText(BaseTools.getFrom("name", data));
				ising.setImageResource(BaseTools.getFrom("is_expire", data,
						"false").equals("false") ? R.drawable.z_ing
						: R.drawable.z_over);
				award.setText(Html.fromHtml("活动奖励：<font color='#E95847'> "
						+ BaseTools.getFrom("activityAeward", data) + "</font>"));
				hdtime.setText(Html.fromHtml("发布时间：<font color='#E95847'> "
						+ BaseTools.getFrom("startDate", data) + " 至 "
						+ BaseTools.getFrom("endDate", data) + "</font>"));
				String sjPhone = BaseTools.getFrom("cp.service_phone", data);
				String summary = BaseTools.getFrom("cp.summary", data);
				sjinfo.setText((StringUtil.isEmpty(summary) ? "" : summary
						+ "\n \n")
						+ "文章版权归【51赚零花钱官网】所有，如有任何疑问请联系官方"
						+ (StringUtil.isEmpty(sjPhone) ? "。" : "，客服电话"
								+ sjPhone + "。"));
				String jtsjStr = BaseTools.getFrom("earnTime", data);
				jtsj.setVisibility(StringUtil.isEmpty(jtsjStr) ? View.GONE
						: View.VISIBLE);
				jtsj.setText(Html.fromHtml("具体时间：<font color='#E95847'> "
						+ jtsjStr + "</font>"));
				Integer jbfee = Integer.parseInt(BaseTools.getFrom("fees",
						data, "0"));
				hdewjl.setText(Html.fromHtml("额外奖励：<font color='#E95847'> "
						+ jbfee + "金币</font>"));
				hdewjl.setVisibility(jbfee > 0 ? View.VISIBLE : View.GONE);
				ewlay.setVisibility(jbfee > 0 ? View.VISIBLE : View.GONE);
				ewinfo.setText(Html
						.fromHtml("参与本活动可额外获得：<font color='#E95847'> " + jbfee
								+ "</font>金币奖励"));
				adapter.notifyDataSetChanged();
				scrollView.smoothScrollTo(0, 0);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.huodong_detail);

		tixian_submit = (Button) findViewById(R.id.tixian_submit);
		tixian_submit.setOnClickListener(this);
		jiangli_submit = (Button) findViewById(R.id.jiangli_submit);
		jiangli_submit.setOnClickListener(this);
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.INVISIBLE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.GONE);
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		TextView leftText = (TextView) findViewById(R.id.content_header_left_text);
		leftText.setVisibility(View.GONE);

		String title = getIntent().getExtras().getString("title");
		centerText.setText(title);
		id = getIntent().getExtras().getString("id");

		scrollView = (ScrollView) findViewById(R.id.hd_detail_scroll);
		listView = (ListView) findViewById(R.id.hd_detail_listview);
		topImg = (RoundAngleImageView) findViewById(R.id.hd_detail_top_image);
		topName = (TextView) findViewById(R.id.hd_detail_top_name);
		topTitle = (TextView) findViewById(R.id.hd_detail_top_title);
		hdtime = (TextView) findViewById(R.id.hd_detail_top_hdtime);
		award = (TextView) findViewById(R.id.hd_detail_top_dhaward);
		join = (TextView) findViewById(R.id.hd_detail_top_join);
		sjinfo = (TextView) findViewById(R.id.hd_detail_sjinfo);
		jtsj = (TextView) findViewById(R.id.hd_detail_top_dhjtsj);
		hdewjl = (TextView) findViewById(R.id.hd_detail_top_dhewjl);
		ewlay = (LinearLayout) findViewById(R.id.hd_detail_walay);
		ewinfo = (TextView) findViewById(R.id.hd_detail_walay_text);
		ewbtn = (TextView) findViewById(R.id.hd_detail_walay_btn);
		txtxbtn = (TextView) findViewById(R.id.hd_detail_txtx_btn);
		ising = (ImageView) findViewById(R.id.hd_detail_top_ising);

		ewbtn.setOnClickListener(this);
		txtxbtn.setOnClickListener(this);

		adapter = new HdDetailAdapter(activity, listitems, id);
		listView.setAdapter(adapter);
		listView.setDividerHeight(0);

		ads = (LinearLayout) findViewById(R.id.hd_detail_ads);
		// 实例化广告条
		AdView adView = new AdView(this, "2422493");
		// 将广告条加入到布局中
		ads.addView(adView);

		loadData(null);

	}

	@Override
	public void loadData(Map<String, String> param) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("id", id);
		HttpService.getByText(URLs.HUODONG_DETAIL, params,
				new TextHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, Header[] arg1,
							String resultStr) {
						try {
							if (BuildConfig.DEBUG)
								Log.i("==result post==", resultStr);
							JSONObject result = new JSONObject(resultStr);

							if (result.getInt("status") == 0) {
								data = result.getJSONObject("data");
								pID = data.getString("_id");
								JSONArray list = data
										.getJSONArray("contentList");
								for (int i = 0; list != null
										&& i < list.length(); i++) {
									listitems.add(list.getJSONObject(i));
								}
							} else {
								if (pageIndex > 0)
									pageIndex--;
							}
							mHandler.sendEmptyMessage(1);

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int arg0, Header[] arg1, String arg2,
							Throwable arg3) {
						UIUtil.ToastMessage(activity, "抱歉，访问失败");
					}
				});
	}

	@Override
	public void clearData() {
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 101) {
			if (appContext.isLogin()) {
				onClick(ewbtn);
			} else {
				UIUtil.ToastMessage(activity, "未登录!");
			}
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		case R.id.hd_detail_walay_btn:
			if (appContext.isLogin()) {
				Bundle data = new Bundle();
				data.putString("id", id);
				UIUtil.openActivity(activity, EWaiJlActivity.class, data);
			} else {
				UIUtil.openActivity(this, LoginActivity.class, null, 101);
			}
			break;
		case R.id.hd_detail_txtx_btn:
			if (appContext.isLogin()) {
				Bundle data = new Bundle();
				data.putString("pID", pID);

				UIUtil.openActivity(activity, WebTiXianTXActivity.class, data);
			} else {
				UIUtil.openActivity(this, LoginActivity.class, null, 101);
			}
			break;
		case R.id.tixian_submit:
			Bundle data1 = new Bundle();
			data1.putString("title", "赚零花钱");
			data1.putString("url",
					"http://m.51zlhq.com/member/timer?pid=50003936");
			UIUtil.openActivity(activity, CommonWebActivity.class, data1);
			break;
		case R.id.jiangli_submit:
			Bundle data = new Bundle();
			data.putString("title", "赚零花钱");
			data.putString("url", "http://m.51zlhq.com/gold/reqApplyfees?pid=50003936");
			UIUtil.openActivity(activity, CommonWebActivity.class, data);
			break;
		default:
			break;
		}
	}

}
